package server

import (
	"gitee.com/yuya000/cards-game-server-go/utils"
	"github.com/BurntSushi/toml"
)

var Conf Configuration

type Configuration struct {
	loaded       bool
	Title        string
	LoggerFormat string `toml:"logger_format"`
	LoggerOut    string `toml:"logger_out"`
	LoggerLevel  string `toml:"logger_level"`
	RunMode      string `toml:"run_mode"`
	Server       ServerConf
	Var          VarConf
	Db           DbConf
	Redis        RedisConf
}

type ServerConf struct {
	Listen           string         `toml:"tcp_listen"`
	ConnDeadLine     utils.Duration `toml:"conn_deadline"`
	ConnReadTimeout  utils.Duration `toml:"conn_read_timeout"`
	ConnWriteTimeout utils.Duration `toml:"conn_write_timeout"`
	EnableTLS        bool           `toml:"enable_tls"`
	CertFilePath     string         `toml:"cert_file_path"`
	KeyFilePath      string         `toml:"key_file_path"`
	ClientCertDir    string         `toml:"client_cert_dir"`
	ClientAuthType   string         `toml:"client_auth_type"`
	MaxConns         uint64         `toml:"max_conns"`
}

type RedisConf struct {
	Network      string
	Addr         string
	ConnTimeout  utils.Duration `toml:"conn_timeout"`
	InitDb       int            `toml:"init_db"`
	Password     string
	ReadTimeout  utils.Duration `toml:"read_timeout"`
	WriteTimeout utils.Duration `toml:"write_timeout"`
	PoolMaxIdle  int            `toml:"pool_max_idle"`
	Wait         bool           `toml:"wait"`
	MaxActive    int            `toml:"max_active"`
	IdleTimeout  utils.Duration `toml:"idel_timeout"`
}

type DbConf struct {
	RunMode         string         `toml:"run_mode"`
	DataSourceName  string         `toml:"data_source_name"`
	MaxOpenConns    int            `toml:"max_open_conns"`
	MaxIdleConns    int            `toml:"max_idle_conns"`
	ConnMaxLifetime utils.Duration `toml:"conn_max_lifetime"`
}

type VarConf struct{}

func LoadConfigurationFromFile(fileName string) error {
	_, err := toml.DecodeFile(fileName, &Conf)
	if err == nil {
		Conf.loaded = true
	}
	return err
}
